﻿Public Class BarCodeCell

    Private DefaultColor As Drawing.Color = Color.Yellow
    Private InputColor As Drawing.Color = Color.Cyan
    Private ErrorColor As Drawing.Color = Color.Red

    Private fIsValid As Boolean = True
    Private fKeepEdit As Boolean = False

    Event InputCompleted(ByVal Sender As Object)

    ' ''' <summary>
    ' ''' デフォルト背景色を設定する
    ' ''' </summary>
    ' ''' <value></value>
    ' ''' <returns></returns>
    ' ''' <remarks></remarks>
    'Public Overrides Property BackColor As System.Drawing.Color
    '    Get
    '        Return MyBase.BackColor
    '    End Get
    '    Set(ByVal value As System.Drawing.Color)
    '        DefaultColor = value
    '        MyBase.BackColor = DefaultColor
    '    End Set
    'End Property

    ''' <summary>
    ''' 入力中の背景色を設定する
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public Property InputBackColor As Drawing.Color
        Get
            Return InputColor
        End Get
        Set(ByVal value As Drawing.Color)
            InputColor = value
        End Set
    End Property

    ''' <summary>
    ''' 値が無効の時の背景色を設定する
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public Property ErrorBackColor As Drawing.Color
        Get
            Return ErrorColor
        End Get
        Set(ByVal value As Drawing.Color)
            ErrorColor = value
        End Set
    End Property

    ''' <summary>
    ''' 値が無効の時の背景色を設定する
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public Shadows Property DefaultBackColor As Drawing.Color
        Get
            Return DefaultColor
        End Get
        Set(ByVal value As Drawing.Color)
            DefaultColor = value
        End Set
    End Property

    ''' <summary>
    ''' 値が有効するかどうかを示す
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property IsValid As Boolean
        Get
            IsValid = fIsValid
        End Get
    End Property

    ''' <summary>
    ''' 値の取得または設定する
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Property Text As String
        Get
            Text = txtInput.Text
        End Get
        Set(ByVal value As String)
            txtInput.Text = value
        End Set
    End Property

    ''' <summary>
    ''' 値を無効にする
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub SetCellError()
        fIsValid = False
        Me.BackColor = ErrorColor
    End Sub

    ''' <summary>
    ''' 値を有効にする
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RestoreCellError()
        fIsValid = True
        Me.BackColor = DefaultColor
    End Sub

    ''' <summary>
    ''' 入力開始
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub BeginInput()
        txtInput.Enabled = True
        txtInput.Focus()
        txtInput.SelectAll()
        Me.BackColor = InputColor
    End Sub

    ''' <summary>
    ''' 編集状態を維持する
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property KeepEdit As Boolean
        Get
            Return fKeepEdit
        End Get
        Set(ByVal value As Boolean)
            fKeepEdit = value
        End Set
    End Property

    Private Sub BarCodeCell_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.BackColorChanged
        txtInput.BackColor = Me.BackColor
    End Sub

    Private Sub BarCodeCell_FontChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.FontChanged
        txtInput.Font = Me.Font
        txtInput.Height = Me.Font.Size + 4
        txtInput.Left = 0
        txtInput.Top = CInt((Me.Height - txtInput.Height) / 2)
        txtInput.Width = Me.Width
    End Sub

    'Private Sub txtInput_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.Leave
    '    MsgBox("leaved")
    'End Sub

    Protected Overridable Sub txtInput_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.LostFocus
        If fKeepEdit Then
            txtInput.Focus()
            Exit Sub
        End If
        txtInput.Enabled = False
        RestoreCellError()
    End Sub

    Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
        '頭に付いている'%'を消す
        Static Dim fIsPercentInput As Boolean = False
        If e.KeyChar = CChar("%") OrElse e.KeyChar = CChar("％") Then
            fIsPercentInput = True
            e.Handled = True
            Exit Sub
        End If
        If fIsPercentInput Then
            fIsPercentInput = False
            e.Handled = True
            Exit Sub
        End If

        If e.KeyChar = vbCr Then
            Dim txtData As TextBox = CType(sender, TextBox)
            txtData.Enabled = False
            fKeepEdit = False
            RaiseEvent InputCompleted(Me)
            e.Handled = True
        End If
    End Sub

    'Private Sub BarCodeCell_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '    Me.Font = Me.Parent.Font
    'End Sub

    Private Sub BarCodeCell_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        txtInput.Left = 0
        txtInput.Top = CInt((Me.Height - txtInput.Height) / 2)
        txtInput.Width = Me.Width
    End Sub

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        Me.BeginInput()
        MyBase.OnClick(e)
    End Sub

    Public Sub Clear()
        Me.BackColor = DefaultColor
        txtInput.Clear()
        fIsValid = True
        fKeepEdit = False
    End Sub
End Class
